<?php

abstract class Core_Data_GenericModel {

    private $Connection;
    private $arrRecords = array ();
    private $strTableName;
    private $arrFieldNames;

    function __construct ( $Connection, $strTableName, $arrFieldNames ) {
        $this->Connection = $Connection;
        $this->strTableName = $strTableName;
        $this->arrFieldNames = $arrFieldNames;
    }

    /**
     * Inserts a record into the table.
     *
     * @param object $objRecord
     * @return integer The insert key, if any.
     */
    public function insert ( $objRecord ) {

        $strClassName = "Model_" . $this->getTableName() . "_Record";
        if ( $objRecord instanceof $strClassName ) {
            $strQuery = "INSERT INTO `" . $this->getTableName() . "` SET ";
            $c = false;
            foreach ( $this->arrFieldNames as $strFieldName ) {
                if ( $objRecord->getUpdateValue( $strFieldName ) ) {
                    $strQuery .= "`" . $strFieldName . "` = `" . $this->Connection->escape( $objRecord->getUpdateValue( $strFieldName ) ) . "`, ";
                }
            }
            $strQuery = substr( $strQuery, 0, -2 );
            $this->Connection->query( $strQuery );
        } else {
            die( "Only insertion of single records are supported at this time." );
        }

    }

    abstract public function isRequired ( $strFieldName );

    abstract public function getDataType ( $strFieldName );

    abstract public function getSize( $strFieldName );

    private function getTableName () {
        return $strTableName;
    }

    public function update () {

        $mixArg1 = func_get_arg(0);

        $strClassName = "Model_" . $this->getTableName() . "_Record";
        if ( $mixArg1 instanceof $strClassName ) {

            // We've now established that this is a record, so lets rename it accordingly.
            $objRecord = $mixArg1;

            // Create the query.
            $strQuery = "UPDATE `" . $this->getTableName() . "` SET ";
            $c = false;
            foreach ( $this->arrFieldNames as $strFieldName ) {
                if ( $objRecord->getUpdateValue( $strFieldName ) ) {
                    $strQuery .= "`" . $strFieldName . "` = `" . $this->Connection->escape( $objRecord->getUpdateValue( $strFieldName ) ) . "`, ";
                }
            }
            $strQuery = substr( $strQuery, 0, -2 );
            $strQuery .= " WHERE ";

            // Send the query.
            $this->Connection->query( $strQuery );

        } else {
            die( "Only updates of single records are supported at this time." );
        }

    }





    public function create () {

    }

    public function find ( array $arrMatches ) {

    }

    public function fintOne () {
        
    }

}

?>
